import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import layout from '../components/layout.vue'
import login from '../views/login/index.vue'
import register from '../views/register/index.vue'
import bafor_layout from '@/components/bafor_layout.vue'
import bafor_home from '@/views/bafor_home/index.vue'
import forumCenter from '@/views/ForumCenter/index.vue'
import forumList from '@/views/ForumCenter/cpn/main.vue'
import forumDetail from '@/views/ForumCenter/cpn/detail.vue'
import bafor_info from '@/views/bafor_info/bafor_info.vue'
import bafor_grade from '@/views/bafor_grade/index.vue'
import bafor_forum from '@/views/bafor_forum/index.vue'
import bafor_course from '@/views/bafor_course/index.vue'
import after_home from '@/views/after_home/index.vue'
import goodqualityourse from '@/views/goodqualityourse/index.vue'
import learncenter from '@/views/learncenter/index.vue'
import corusePanel from '@/views/learncenter/cpn/corusePanel.vue'
import exam from '@/views/learncenter/cpn/exam.vue'
import score from '@/views/learncenter/cpn/score.vue'
import userInfo from '@/views/learncenter/cpn/userInfo.vue'
import { Message } from 'element-ui';
Vue.use(VueRouter)
// 解决报错 此方法只针对VueRouter3.0以上
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    name: 'bafor',
    redirect: '/home',
    component: bafor_layout,
    children: [
      {
        path: 'home',
        name: 'home',
        component: bafor_home
      },
      {
        path: 'learningCenter',
        name: 'learningCenter',
        component: learncenter,
        children: [{
          path: '/',
          name: 'corusePanel',
          component: corusePanel,
        },
        // {
        //   path: '/learningCenter/exam',
        //   name: 'exam',
        //   component: exam,
        // },
        {
          path: '/learningCenter/score',
          name: 'score',
          component: score,
        },
        {
          path: '/learningCenter/userInfo',
          name: 'userInfo',
          component: userInfo,
        },
        ]
      },
      {
        path: 'GoodQualityourse',
        name: 'GoodQualityourse',
        component: goodqualityourse
      },
      {
        path: 'forumCenter',
        name: 'forumCenter',
        redirect: 'forumCenter/List',
        component: forumCenter,
        children: [{
          path: 'List',
          name: 'forumList',
          component: forumList,
        },
        {
          path: 'List/:id',
          name: 'forumDetail',
          component: forumDetail,
        }]

      },

    ]
  },
  {
    path: '/after',
    name: 'layout',
    component: layout,
    children: [
      {
        path: 'home',
        name: 'after_home',
        component: after_home,
      }, {
        path: 'info',
        name: 'bafor_info',
        component: bafor_info,
      },
      {
        path: 'grade',
        name: 'bafor_grade',
        component: bafor_grade,
      },
      {
        path: 'forum',
        name: 'bafor_forum',
        component: bafor_forum,
      }, {
        path: 'course',
        name: 'bafor_course',
        component: bafor_course,
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: login
  }, {
    path: '/register',
    name: 'register',
    component: register
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  const { token } = store.state.user.profile
  if (to.path.startsWith("/after") && !token) {
    Message({
      type: 'warning',
      message: "还没有登录！请登录！"
    })
    next({
      path: '/login'
    })
  }
  next()
})
export default router
